package com.yjps.house.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yjps.common.message.Message;
import com.yjps.house.entity.AgentForQuery;
import com.yjps.house.service.AgentService;
import com.yjps.system.model.Agent;
import com.yjps.system.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/house/agent")
public class AgentController {
    @Autowired
    AgentService agentService;

    /**
     * @param json {
    "address": {
    "province": "",
    "city": "",
    "county": "",
    "town": ""
    },
    "page": "",
    "size": ""
    }
     * @param json
     * @param respone
     * @return
     */
    @RequestMapping(value = "/query.do",method = RequestMethod.POST)
    @ResponseBody
    public String query(@RequestParam(value = "json") String json,HttpServletResponse respone){
        //解析json
        JSONObject object= JSON.parseObject(json);
        int size=Integer.parseInt(object.getString("size"));
        int startRow=(Integer.parseInt(object.getString("page"))-1)*size;
        Map<String,Object> addressMap=(Map<String,Object>) JSON.parse(object.getString("address"));
        List<Agent> agents=agentService.query(addressMap);
        List<AgentForQuery> agentForQueries=agentService.agentForQueryList(agents);
        Message message;

        if (agentForQueries!=null&&!agentForQueries.isEmpty()){
            int page =agentService.getPages(agentForQueries,size);
            List<AgentForQuery> subAgents=agentService.subList(agentForQueries, startRow, size);
            Map<String,Object> map=new HashMap<>();
            map.put("countPage",page);
            map.put("agentList",subAgents);
            message=new Message(true,"query something successfully",map);
        }else{
            message=new Message(false,"query nothing");
        }
//        respone.setHeader("Access-Control-Allow-Origin","*");
        return message.toJson();
    }

    /**
     * 根据房源id查询经纪人
     * @param houseId
     * @param respone
     * @return
     */
    @RequestMapping(value = "/queryByHouse.do",method = RequestMethod.POST)
    @ResponseBody
    public String queryByHouse(@RequestParam(value = "houseId") String houseId,HttpServletResponse respone){
        int id=Integer.parseInt(houseId);
        Agent agent=agentService.queryByHouseId(id);
        Message message;
        if (agent!=null){
            User user=agentService.queryByAgent(agent.getUserId());
            Map<String,Object> map=new HashMap<>();
            map.put("agent",agent);
            map.put("user",user);
            message=new Message(true,"query a agent successfully",map);
        }else {
            message=new Message(false,"query failed");
        }
//        respone.setHeader("Access-Control-Allow-Origin","*");
        return message.toJson();
    }

    /**
     * 获取门店下的所有经纪人
     * @param shopId
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/listByShop.do",method = RequestMethod.POST)
    @ResponseBody
    public String queryByShop(@RequestParam(value = "shopId") String shopId,
                               @RequestParam(value = "page") String page,
                               @RequestParam(value = "pageSize") String pageSize){
        int size=Integer.parseInt(pageSize);
        int startRow=(Integer.parseInt(page)-1)*size;
        List<Agent> agents=agentService.ListbyShop(Integer.parseInt(shopId), startRow, size);
        List<AgentForQuery> agentForQueries=agentService.agentForQueryList(agents);
        Message message;
        if (agents!=null&&!agents.isEmpty()){
            message=new Message(true,"query something",agentForQueries);
        }else if (agents!=null){
            message=new Message(true,"query nothing");
        }else {
            message=new Message(false,"failed");
        }
        //respone.setHeader("Access-Control-Allow-Origin","*");
        return message.toJson();
    }

    /**
     * 门店经理关联一个经纪人
     * @param shopId
     * @param phone
     * @return
     */
    @RequestMapping(value = "/relate.do",method = RequestMethod.POST)
    @ResponseBody
    public String queryByShop(@RequestParam(value = "shopId") String shopId,
                              @RequestParam(value = "phone") String phone){
        agentService.relateAgent(phone, Integer.parseInt(shopId));
        Message message=new Message(true,"success");
        //respone.setHeader("Access-Control-Allow-Origin","*");
        return message.toJson();
    }

    /**
     * 门店经理删除经纪人
     * @param agentId
     * @return
     */
    @RequestMapping(value = "/delete.do",method = RequestMethod.POST)
    @ResponseBody
    public String queryByShop(@RequestParam(value = "agentId") String agentId){
        boolean flag=agentService.deleteAgent(Integer.parseInt(agentId));
        Message message;
        if (flag){
            message=new Message(true,"success");
        }else {
            message=new Message(false,"failed");
        }
        //respone.setHeader("Access-Control-Allow-Origin","*");
        return message.toJson();
    }

    /**
     * 用户更新经纪人信息
     * @param json
     * @return
     */
    @RequestMapping(value = "/update.do",method = RequestMethod.POST)
    @ResponseBody
    public String update(@RequestParam(value = "json") String json){
        JSONObject object = JSON.parseObject(json);
        Agent agent = JSON.parseObject(object.getString("agent"), Agent.class);
        User user = JSON.parseObject(object.getString("user"), User.class);
        boolean flag=agentService.updateAgent(user,agent);
        Message message;
        if (flag){
            Agent agent2=agentService.queryByUserId(user.getId());
            User user2=agentService.queryByAgent(user.getId());
            Map<String,Object> map=new HashMap<>();
            map.put("agent",agent2);
            map.put("user",user2);
            message=new Message(true,"success",map);
        }else{
            message=new Message(false,"failed");
        }
        //respone.setHeader("Access-Control-Allow-Origin","*");
        return message.toJson();
    }

    /**
     * 用户获取经纪人信息
     * @param userId
     * @return
     */
    @RequestMapping(value = "/get.do",method = RequestMethod.POST)
    @ResponseBody
    public String get(@RequestParam(value = "userId") String userId){
        Agent agent=agentService.queryByUserId(Integer.parseInt(userId));
        User user=agentService.queryByAgent(Integer.parseInt(userId));
        Message message;
        if (agent!=null&&user!=null){
            Map<String,Object> map=new HashMap<>();
            map.put("agent",agent);
            map.put("user",user);
            message=new Message(true,"success",map);
        }else{
            message=new Message(false,"failed");
        }
        //respone.setHeader("Access-Control-Allow-Origin","*");
        return message.toJson();
    }

}
